// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: donggu

#include <cxxopts.hpp>

#include "coin2/app/driver.h"
#include "coin2/base/config/app_config.h"
#include "coin2/base/config/args.h"
#include "coin2/base/log.h"
#include "coin2/base/time.h"
#include "coin2/exchange/base/feed/update.h"
#include "coin2/feed/mgr/system.h"
#include "coin2/strategy/strategy.h"


using namespace coin2::exchange::base::symbology;
using namespace coin2::base::config;
using namespace coin2::exchange::feed;
using namespace coin2::app;
using coin2::exchange::base::feed::FeedUpdate;
using coin2::exchange::base::order::OrderUpdate;

class CountStrategy : public ::coin2::strategy::IStrategy {
 public:
  void onTradeFeed(const FeedUpdate& /*upd*/) override { trade_++; }
  void onBookFeed(const FeedUpdate& /*upd*/) override { book_++; }

  void onAccountInfo(const OrderUpdate& /*upd*/) override {}
  void onAccountOrder(const OrderUpdate& /*upd*/) override {}
  void onOrderLog(const coin::proto::OrderGatewayLog& log) override { order_log_++; }

 public:  // public for convenience
  uint64_t trade_ {};
  uint64_t book_ {};
  uint64_t order_log_ {};
};

int main(int argc, char* argv[]) {
  google::InitGoogleLogging(argv[0]);

  DriverConfig app;

  cxxopts::Options opt("Counter", "Count feed and order events");
  AddDriverOptions(&opt);
  AddIntervalOptions(&opt);
  AddProductOptions(&opt);

  auto res = opt.parse(argc, argv);
  ParseDriverOptions(res, &app);
  ParseIntervalOptions(
      res,
      app.mutable_feed()->mutable_common()->mutable_archive()->mutable_interval());
  ParseProductOptions(res, app.mutable_feed());

  LOG(INFO) << app.DebugString();

  CountStrategy strat;
  ::coin2::app::Driver driver(app, &strat);
  driver.Run();

  printf("    trade: %9ld\n", strat.trade_);
  printf("     book: %9ld\n", strat.book_);
  printf("order_log: %9ld\n", strat.order_log_);

  return 0;
}
